#include "view_time_4.h"
#include "view_time_4.inc"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font_mngr.h"
//#include "view_common.h"
#include "app_trace.h"



#define P_WEEKDAY_X_POS 0
#define P_WEEKDAY_Y_POS 148
#define P_BLE_ICON_X_POS 0
#define P_BATT_ICON_X_POS 55

#define P_AM_PM_ICON_X_POS 0
#define P_AM_PM_ICON_Y_POS 0
#define P_BLE_ICON_Y_POS 0
#define P_BATT_ICON_Y_POS 0

#define P_MIN_HIGH_X_POS 13
#define P_MIN_LOW_X_POS 43

#define P_HOUR_HIGH_X_POS 13
#define P_HOUR_LOW_X_POS 43

#define P_HOUR_HIGH_Y_POS 26
#define P_HOUR_LOW_Y_POS 26
#define P_MIN_HIGH_Y_POS 85
#define P_MIN_LOW_Y_POS 85

#define P_DATE_MON_HIGH_X_POS           44
#define P_DATE_MON_HIGH_Y_POS           148
#define P_DATE_MON_LOW_X_POS            52
#define P_DATE_MON_LOW_Y_POS            148
#define P_DATE_DAY_HIGH_X_POS           66
#define P_DATE_DAY_HIGH_Y_POS           148
#define P_DATE_DAY_LOW_X_POS            74
#define P_DATE_DAY_LOW_Y_POS            148
#define P_DATE_SLASH_X_POS              59
#define P_DATE_SLASH_Y_POS              148

void view_time_4_display(void)
{
    bool is_ble_connected = false;
    int hour = 0;
    int minute = 0;
    int month = 0;
    int day = 0;
    int week_index = 0;
    bool is_charge = false;
    int battery_percent = 0;
    int show_index = 0;
    int battery_level = 0;
    int pm_type = 0;

    is_ble_connected = disp_ctrl.com_data.u_temp[1];
    hour = disp_ctrl.com_data.u_temp[6];
    minute = disp_ctrl.com_data.u_temp[7];
    month = disp_ctrl.com_data.u_temp[4];
    day = disp_ctrl.com_data.u_temp[5];
    week_index = disp_ctrl.com_data.u_temp[9] - 1;
    is_charge = disp_ctrl.com_data.u_temp[2];
    battery_percent = disp_ctrl.com_data.u_temp[3];
    show_index = disp_ctrl.auto_refresh_count;
    battery_level = battery_ctrl_get_level(battery_percent);
    pm_type = disp_ctrl.com_data.u_temp[8];
#if 0
#warning debug
    is_ble_connected = true;
    is_charge = true;
    pm_type = 0;
#endif
    draw_on_chip_unzip_image(P_WEEKDAY_X_POS, P_WEEKDAY_Y_POS, home_weekday[week_index]);
    if (pm_type)
    {
        draw_on_chip_unzip_image(P_AM_PM_ICON_X_POS, P_AM_PM_ICON_Y_POS, g_home_am_pm[pm_type - 1]);
    }

    if (is_ble_connected)
    {
        if (pm_type)
        {
            draw_on_chip_unzip_image(42, P_BLE_ICON_Y_POS, g_home_ble_icon);
        }
        else
        {
            draw_on_chip_unzip_image(0, P_BLE_ICON_Y_POS, g_home_ble_icon);
        }
    }

    draw_on_chip_unzip_image(P_BATT_ICON_X_POS, P_BATT_ICON_Y_POS, g_batt_box);
    if (is_charge)
    {
        draw_on_chip_unzip_gradients_image(P_BATT_ICON_X_POS + 2, P_BATT_ICON_Y_POS + 1, home_batt[show_index % 5], 0x8fee, 0x57f5, RGB565_COLOR_BLACK);
    }
    else
    {
        draw_on_chip_unzip_gradients_image(P_BATT_ICON_X_POS + 2, P_BATT_ICON_Y_POS + 1, home_batt[battery_level], 0x8fee, 0x57f5, RGB565_COLOR_BLACK);
    }

    draw_on_chip_unzip_gradients_image(P_HOUR_HIGH_X_POS, P_HOUR_HIGH_Y_POS, home_num[hour / 10], 0x8fee, 0x57f5, RGB565_COLOR_BLACK);
    draw_on_chip_unzip_gradients_image(P_HOUR_LOW_X_POS, P_HOUR_LOW_Y_POS, home_num[hour % 10], 0x8fee, 0x57f5, RGB565_COLOR_BLACK);

    draw_on_chip_unzip_gradients_image(P_MIN_HIGH_X_POS, P_MIN_HIGH_Y_POS, home_num[minute / 10], 0x47f7, 0x17fe, RGB565_COLOR_BLACK);
    draw_on_chip_unzip_gradients_image(P_MIN_LOW_X_POS, P_MIN_LOW_Y_POS, home_num[minute % 10], 0x47f7, 0x17fe, RGB565_COLOR_BLACK);

    draw_on_chip_unzip_image(P_DATE_MON_HIGH_X_POS, P_DATE_MON_HIGH_Y_POS, g_small_num_6x12[month / 10]);
    draw_on_chip_unzip_image(P_DATE_MON_LOW_X_POS, P_DATE_MON_LOW_Y_POS, g_small_num_6x12[month % 10]);
    draw_on_chip_unzip_image(P_DATE_DAY_HIGH_X_POS, P_DATE_DAY_HIGH_Y_POS, g_small_num_6x12[day / 10]);
    draw_on_chip_unzip_image(P_DATE_DAY_LOW_X_POS, P_DATE_DAY_LOW_Y_POS, g_small_num_6x12[day % 10]);
    draw_on_chip_unzip_image(P_DATE_SLASH_X_POS, P_DATE_SLASH_Y_POS, g_date_slash);
}
